<?php
/***************************************************************************
 *   copyright				: (C) 2008, 2009 WeBid
 *   site					: http://www.webidsupport.com/
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version. Although none of the code may be
 *   sold. If you have been sold this script, get a refund.
 ***************************************************************************/

include 'includes/common.inc.php';
//include 'includes/class.images.helper.php';
if (!$user->is_logged_in())
{
	//if your not logged in you shouldn't be here
	header("location: user_login.php");
	exit;
}

$cropdefault = false;
$width = $system->SETTINGS['thumb_show'];
$height = $width / 1.2;
$watermark_img = $incurl . 'themes/' . $system->SETTINGS['theme'] . '/water_logo.gif';

function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale)
{
	list($imagewidth, $imageheight, $imageType) = getimagesize($image);
	$imageType = image_type_to_mime_type($imageType);
	// some error checks
	$start_width = ($start_width < 0) ? 0 : $start_width;
	$start_height = ($start_height < 0) ? 0 : $start_height;
	$width = ($imagewidth < $width) ? $imagewidth : $width;
	$height = ($imageheight < $height) ? $imageheight : $height;
	if (($width + $start_width) > $imagewidth)
	{
		$start_width = $imagewidth - $width;
	}
	if (($height + $start_height) > $imageheight)
	{
		$start_height = $imageheight - $height;
	}
	
	/*
	$maxHeight = 100;
	if (($height * $scale) > $maxHeight) {
		if ($width > $height) {
			$width = 120/$scale;
			$height = 100/$scale;
		} else {
			$width = 100/$scale;
			$height = 120/$scale;
		}
		
	}
	
	
	$newImageWidth = ceil($width * $scale);
	$newImageHeight = ceil($height * $scale);
	*/
	
	$scw = $width / 120;
	$sch = $height / 100;
	
	if ($width > $height)
	{
		$newImageWidth = ceil($width/$scw-0.5);
		$newImageHeight = ceil($height/$scw-0.5);
	}
	else
	{
		$newImageWidth = ceil($width/$sch);
		$newImageHeight = ceil($height/$sch);
	}
	
	
	
	$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
	// make the background white
	$bg = imagecolorallocate($newImage, 0, 0, 0);
	imagefill($newImage, 0, 0, $bg);
	switch ($imageType)
	{
		case 'image/gif':
			$source = imagecreatefromgif ($image);
			break;
		case 'image/pjpeg':
		case 'image/jpeg':
		case 'image/jpg':
			$source = imagecreatefromjpeg($image);
			break;
		case 'image/png':
		case 'image/x-png':
			$source = imagecreatefrompng($image);
			break;
	}
	imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height);
	switch ($imageType)
	{
		case 'image/gif':
			imagegif ($newImage, $thumb_image_name);
			break;
		case 'image/pjpeg':
		case 'image/jpeg':
		case 'image/jpg':
			imagejpeg($newImage, $thumb_image_name, 90);
			break;
		case 'image/png':
		case 'image/x-png':
			imagepng($newImage, $thumb_image_name);
			break;
	}
	chmod($thumb_image_name, 0777);
	return $thumb_image_name;
}

// Process delete
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['img']))
{
	if ($_SESSION['SELL_pict_url_temp'] == $_SESSION['UPLOADED_PICTURES'][intval($_GET['img'])])
	{
		unlink($upload_path . session_id() . '/' . $_SESSION['SELL_pict_url']);
		unset($_SESSION['SELL_pict_url']);
	}
	unlink($upload_path . session_id() . '/' . $_SESSION['UPLOADED_PICTURES'][intval($_GET['img'])]);
	unset($_SESSION['UPLOADED_PICTURES'][intval($_GET['img'])]);
	unset($_SESSION['UPLOADED_PICTURES_SIZE'][intval($_GET['img'])]);

	$firstPic = reset($_SESSION['UPLOADED_PICTURES']);
	if($firstPic) {
		$scale = 1;
		$large_image_location = $upload_path . session_id() . '/' . $firstPic;
		$thumb_image_location = $upload_path . session_id() . '/thumb-' . $firstPic;
		//$width      = ImagesHelper::width($large_image_location);
		//$height     = ImagesHelper::height($large_image_location);
		 
		$cropped = resizeThumbnailImage($thumb_image_location, $large_image_location, 100000, 100000, 0, 0, $scale);
		$_SESSION['SELL_pict_url'] = 'thumb-' . $firstPic;
		$_SESSION['SELL_pict_url_temp'] = $firstPic;
	}
	
	echo '
	<html>
		<head>
			<script type="text/javascript">
				window.parent.delPic("' . $_GET['img'] . '");
			</script>
		</head>
	</html>';
	exit;
	
}

if (isset($_GET['action']) && $_GET['action'] == 'makedefault')
{
	$cropdefault = true;
	$image = $_GET['img'];
}

if (isset($_GET['action']) && $_GET['action'] == 'crop' && !empty($_POST['w']) && $_POST['w'] != 0)
{
	if ($_POST['upload_thumbnail'] == $MSG['616'])
	{
		// Get the new coordinates to crop the image.
		$x1 = intval($_POST['x1']);
		$y1 = intval($_POST['y1']);
		$x2 = intval($_POST['x2']);
		$y2 = intval($_POST['y2']);
		$w = intval($_POST['w']);
		$h = intval($_POST['h']);
		// Scale the image to the thumb_width set above
		$scale = $width / $w;
		$large_image_location = $upload_path . session_id() . '/' . $_GET['img'];
		$thumb_image_location = $upload_path . session_id() . '/thumb-' . $_GET['img'];
		$cropped = resizeThumbnailImage($thumb_image_location, $large_image_location, $w, $h, $x1, $y1, $scale);
		$_SESSION['SELL_pict_url'] = 'thumb-' . $_GET['img'];
		$_SESSION['SELL_pict_url_temp'] = $_GET['img'];
	}
	else
	{
		$_SESSION['SELL_pict_url_temp'] = $_SESSION['SELL_pict_url'] = $_GET['img'];
	}
}

// close window
if (!empty($_POST['creategallery']))
{
	echo '<script type="text/javascript">window.close()</script>';
	exit;
}

// PROCESS UPLOADED FILE
if (isset($_POST['uploadpicture']) && $_POST['uploadpicture'] == $MSG['681'])
{
	$canUpload = (!isset($_SESSION['UPLOADED_PICTURES']) || count($_SESSION['UPLOADED_PICTURES']) < $system->SETTINGS['maxpictures']);
	
	if (!empty($_FILES['userfile']['tmp_name']) && $_FILES['userfile']['tmp_name'] != 'none' && $canUpload)
	{
		if (!isset($_SESSION['UPLOADED_PICTURES']) || !is_array($_SESSION['UPLOADED_PICTURES'])) $_SESSION['UPLOADED_PICTURES'] = array();
		if (!isset($_SESSION['UPLOADED_PICTURES_SIZE']) || !is_array($_SESSION['UPLOADED_PICTURES_SIZE'])) $_SESSION['UPLOADED_PICTURES_SIZE'] = array();
		$filename = $_FILES['userfile']['name'];
		$nameparts = explode('.', $filename);
		$ext_key = count($nameparts) - 1;
		$file_ext = $nameparts[$ext_key];
		$file_types = array('gif', 'jpg', 'jpeg', 'png', 'GIF', 'JPG', 'JPEG', 'PNG');

		// clean the name
		unset($nameparts[$ext_key]);
		$newname = implode('_', $nameparts);

		$newname = preg_replace('/[^a-zA-Z0-9_]/', '', $newname);
		$newname .= '.' . $file_ext;

		if ($_FILES['userfile']['size'] > $system->SETTINGS['maxuploadsize'])
		{
			$ERR = $ERR_709 . '&nbsp;' . ($system->SETTINGS['maxuploadsize'] / 1024) . '&nbsp;' . $MSG['672'];
		}
		elseif (!in_array($file_ext, $file_types))
		{
			$ERR = $ERR_710 . ' (' . $file_ext . ')';
		}
		elseif (in_array($newname, $_SESSION['UPLOADED_PICTURES']))
		{
			$ERR = $MGS_2__0054 . ' (' . $_FILES['userfile']['name'] . ')';
		}
		else
		{
			// Create a TMP directory for this session (if not already created)
			if (!file_exists($upload_path . session_id()))
			{
				umask(0);
				mkdir($upload_path . session_id(), 0777);
				chmod($upload_path . session_id(), 0777); //incase mkdir fails
			}

			$newname = strtolower($newname);
			// Move uploaded file into TMP directory & rename
			if ($system->move_file($_FILES['userfile']['tmp_name'], $upload_path . session_id() . '/' . $newname))
			{
				$tmp_fname = $upload_path . session_id() . '/' . $newname;
				include_once('includes/class.images.helper.php');
				$width      = ImagesHelper::width($tmp_fname) / 800; 
				$height     = ImagesHelper::height($tmp_fname) / 600;  

				if ($width > $height)
					$ratio = ceil(100/$width);
				else
					$ratio = ceil(100/$height);
				
				end($_SESSION['UPLOADED_PICTURES']);        
				$imgID = count($_SESSION['UPLOADED_PICTURES']) > 0 ? key($_SESSION['UPLOADED_PICTURES']) + 1 : 0;
				$prefix = $imgID . '_'; // Приставка к имени файла от масштабированного 
				$format = 'jpg'; // формат для сохранения изображения
				   
				if($scaled_image  = ImagesHelper::scale($tmp_fname,$ratio)) {            
					// сохранение полученного изображения            
					$scaled_image = ImagesHelper::overlay($scaled_image, $watermark_img,'right-bottom');
					ImagesHelper::save($scaled_image, $tmp_fname,$prefix,$format);
					@unlink($tmp_fname);
					$newname = $prefix . $newname;
				}

				// Populate arrays
				array_push($_SESSION['UPLOADED_PICTURES'], $newname);
				//end($_SESSION['UPLOADED_PICTURES']);        
				//$imgID = key($_SESSION['UPLOADED_PICTURES']);
				$fname = $upload_path . session_id() . '/' . $newname;
				@chmod($fname, 0777);
				
				array_push($_SESSION['UPLOADED_PICTURES_SIZE'], filesize($fname));
				if (count($_SESSION['UPLOADED_PICTURES']) == 1)
				{
					$cropdefault = true;
					$image = $newname;


					$scale = 1;
					$large_image_location = $upload_path . session_id() . '/' . $newname;
					$thumb_image_location = $upload_path . session_id() . '/thumb-' . $newname;
					//$width      = ImagesHelper::width($large_image_location);
					//$height     = ImagesHelper::height($large_image_location);
					          
					$cropped = resizeThumbnailImage($thumb_image_location, $large_image_location, 100000, 100000, 0, 0, $scale);
					$_SESSION['SELL_pict_url'] = 'thumb-' . $newname;
					$_SESSION['SELL_pict_url_temp'] = $newname;



				}
				
			}
		}
	}

	echo '
	<html>
		<head>
			<script type="text/javascript">
				window.parent.showPostedPic("' . ((isset($ERR)) ? $ERR : '') . '","' . $system->SETTINGS['maxpictures'] . '","' . $imgID . '","' . $uploaded_path . session_id() . '/' . $newname.'",'. count($_SESSION['UPLOADED_PICTURES']) . ');
			</script>
		</head>
	</html>';
	exit;
	
}

if ($cropdefault)
{
	list($imgwidth, $imgheight) = getimagesize($upload_path . session_id() . '/' . $image);
	$swidth = ($imgwidth < 380) ? '' : ' width: 380px;';
	$imgratio = ($imgwidth > 380) ? $imgwidth / 380 : 1;
	$whratio = $imgheight / $imgwidth;
	if ($imgwidth > $imgheight) //landscape
	{
		$ratio = '1.2:1';
		$thumbwh = 'width:' . $width . '; height:' . $height . ';';
		$scaleX = 120;
		$scaleY = 100;
		$startY = 380 * $whratio;
		$startX = $startY * 1.2;
	}
	else //portrait
	{
		$ratio = '1:1.2';
		$thumbwh = 'height:' . $width . '; width:' . $height . ';';
		$scaleX = 100;
		$scaleY = 120;
		$startX = 380 * $whratio;
		$startY = $startX * 1.2;
	}


	$template->assign_vars(array(
			'RATIO' => $ratio,
			'THUMBWH' => $thumbwh,
			'SCALEX' => $scaleX,
			'SCALEY' => $scaleY,
			'IMGRATIO' => $imgratio,
			'SWIDTH' => $swidth,
			'IMGWIDTH' => $imgwidth,
			'IMGHEIGHT' => $imgheight,
			'IMGPATH' => $uploaded_path . session_id() . '/' . $image,
			'STARTX' => $startX,
			'STARTY' => $startY,
			'IMAGE' => $image
			));
}
else
{
	$template->assign_vars(array(
			'MAXIMAGES' => $system->SETTINGS['maxpictures'],
			'ERROR' => (isset($ERR)) ? $ERR : '',

			'B_CANUPLOAD' => (!isset($_SESSION['UPLOADED_PICTURES']) || count($_SESSION['UPLOADED_PICTURES']) < $system->SETTINGS['maxpictures'])
			));
}

foreach ($_SESSION['UPLOADED_PICTURES'] as $k => $v)
{
	$template->assign_block_vars('images', array(
			'IMGNAME' => $v,
			'IMGSIZE' => $_SESSION['UPLOADED_PICTURES_SIZE'][$k],
			'ID' => $k,
			'DEFAULT' => ($v == $_SESSION['SELL_pict_url_temp']) ? 'selected.gif' : 'unselected.gif'
			));
}

if ($system->SETTINGS['fees'] == 'y')
{
	$query = "SELECT value FROM " . $DBPrefix . "fees WHERE type = 'picture_fee'";
	$res = mysql_query($query);
	$system->check_mysql($res, $query, __LINE__, __FILE__);
	$image_fee = mysql_result($res, 0);
}
else
{
	$image_fee = 0;
}

// get decimals for javascript rounder
$decimals = '';
for ($i = 0; $i < $system->SETTINGS['moneydecimals']; $i++)
{
	$decimals .= 0;
}

$template->assign_vars(array(
		'SITENAME' => $system->SETTINGS['sitename'],
		'THEME' => $system->SETTINGS['theme'],
		'NUMIMAGES' => count($_SESSION['UPLOADED_PICTURES']),
		'IMAGE_COST' => $image_fee,
		'FEE_DECIMALS' => $decimals,
		'CHARSET' => $CHARSET,
		'B_CROPSCREEN' => $cropdefault
		));
/*$template->set_filenames(array(
		'body' => 'upldgallery.tpl'
		));
$template->display('body');*/




?>
